package com.eschool.beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.beans.datamodels.TeacherDataModel;
import com.eschool.dao.TeacherDao;
import com.eschool.model.Teacher;
import com.eschool.service.TeacherService;
import com.eschool.service.impl.TeacherServiceImpl;
import com.eschool.util.ConstraintsUtil;

@ManagedBean(name="teacherStatusUpdateBean")
@ViewScoped
public class TeacherStatusUpdateBean {

	private Teacher[] selectedTeachersList;
    private TeacherDataModel teachersModel;
	TeacherService service;	
	private static List<Teacher> teachers;	
	private boolean newStatus;

	public TeacherStatusUpdateBean() {
		teachers = new ArrayList<Teacher>();
		service = new TeacherServiceImpl();
		teachers = TeacherDao.getTeachers(ConstraintsUtil.getConstraintsMap());		
		teachersModel = new TeacherDataModel(teachers);
	}

	public boolean isNewStatus() {
		return newStatus;
	}

	public void setNewStatus(boolean newStatus) {
		this.newStatus = newStatus;
	}
	
	public Teacher[] getSelectedTeachersList() {
		return selectedTeachersList;
	}

	public void setSelectedTeachersList(Teacher[] selectedTeachersList) {
		this.selectedTeachersList = selectedTeachersList;
	}

	public TeacherDataModel getTeachersModel() {
		return teachersModel;
	}

	public void setTeachersModel(TeacherDataModel teachersModel) {
		this.teachersModel = teachersModel;
	}

	public String perform(){
    	
    	List selectedTeachers = Arrays.asList(selectedTeachersList);

    	service.updateTeacherStatus(selectedTeachers, newStatus);
    	
    	FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
    					FacesMessage.SEVERITY_INFO,"Info message", "Teachers' Status Updated"));
    	
    	return "teacherStatusUpdate";
    }
}
